<?php
require_once  __DIR__ . '/includes/authenticate.php';

include_once __DIR__ . '/db_bb_connect.php';


if (isset($_POST['season'])) {
	
$setseason = $_POST['season'];

$sql = "SELECT teams.playerID, users.firstname, users.lastname, teams.playernumber
FROM teams 
JOIN users
ON teams.playerID = users.user_key
WHERE season = '$setseason'
AND users.type = 'Player' 
AND teams.playertype != 'training'
ORDER BY teams.playernumber";

$result=$db->query($sql);

?>

<script type="text/javascript">
function printcumu(el){
	var restorepage = document.body.innerHTML;
	var printcontent = document.getElementById(el).innerHTML;
	document.body.innerHTML = printcontent;
	window.print();
	document.body.innerHTML = restorepage;
}
</script>

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">

<style>

table {
	font-size: 14px;
	}

</style>

<div class="container-fluid">
  <div class="row">
    <div class="col-md-1"></div>
    <div class="col-md-10">

<div class="table-responsive" id="cumustatstable">
<h3>Cumulative Stats of <?= $setseason; ?></h3>
<p>

</p>
<table id="" class="table table-hover table-striped">
    <thead>
      <tr>
      	<th>#</th>
        <th>Name</th>
        <th>-</th>
        <th>DRB</th>
        <th>ORB</th>
        <th>TO</th>
        <th>ST</th>
        <?php if ($setseason >= '2018/2019') { echo "<th>AST</th>";echo "<th>BS</th>"; } ?>
        
        
<?php

if ($setseason < '2017/2018') {
  echo "<th>FG</th>";
} else if ($setseason >= '2017/2018') {
  echo "<th>FGI</th>";
  echo "<th>FGO</th>";
  echo "<th>3P</th>";
}

?>
		<th>FT</th>
        <th>PTS</th>
        <th>PF</th>
        <?php if ($setseason >= '2018/2019') { 
                    echo "<th>MP</th>"; 
                    echo "<th>S5</th>"; 

                  } ?>
        <th>GP</th>
      </tr>
    </thead>
    <tbody>

<?php

while($row=$result->fetch_assoc()){	

	$playerID = $row['playerID'];
	$firstname = $row['firstname'];
	$lastname = $row['lastname'];
	$playernumber = $row['playernumber'];


	$sql2 = "SELECT SUM(ORB) AS SUMORB,
	SUM(DRB) AS SUMDRB,
	SUM(TOV) AS SUMTOV,
	SUM(PF) AS SUMPF,
	SUM(ST) AS SUMST,
	SUM(AST) AS SUMAST,
	SUM(BS) AS SUMBS,
	SUM(FTscored) AS SUMFTscored,
	SUM(FTtotal) AS SUMFTtotal,
	SUM(FGscored) AS SUMFGscored,
	SUM(FGtotal) AS SUMFGtotal,
	SUM(FG_outside_scored) AS SUMFG_outside_scored,
	SUM(FG_outside_total) AS SUMFG_outside_total,
	SUM(THPscored) AS SUMTHPscored,
	SUM(THPtotal) AS SUMTHPtotal,
	SUM(MP) AS SUMMP,
	count(case DNP when 'no' then 1 else null end) AS SUMDNP,
	count(case S5 when 'yes' then 1 else null end) AS SUMS5,
	count(DNP) AS totalgamestatsavailable
	FROM stats 
	WHERE season = '$setseason'
	AND playerID = '$playerID'";

	$result2=$db->query($sql2);
	$row2=$result2->fetch_assoc();


	$SUMORB = $row2['SUMORB'];
	$SUMDRB = $row2['SUMDRB'];
	$SUMTOV = $row2['SUMTOV'];
	$SUMST = $row2['SUMST'];
	$SUMS5 = $row2['SUMS5'];
	$SUMAST = $row2['SUMAST'];
	$SUMBS = $row2['SUMBS'];
	$SUMPF = $row2['SUMPF'];
	$SUMFTtotal = $row2['SUMFTtotal'];
	$SUMFTscored = $row2['SUMFTscored'];
	$SUMFGscored = $row2['SUMFGscored'];
	$SUMFGtotal = $row2['SUMFGtotal'];
	$SUMFG_outside_scored = $row2['SUMFG_outside_scored'];
	$SUMFG_outside_total = $row2['SUMFG_outside_total'];
	$SUMTHPscored = $row2['SUMTHPscored'];
	$SUMTHPtotal = $row2['SUMTHPtotal'];
	$totalgamestatsavailable = $row2['totalgamestatsavailable'];
	$SUMDNP = $row2['SUMDNP'];

	$totalgamesplayed = $row2['SUMDNP'];

	$minutesplayed = floor((int)$row2['SUMMP'] / 60);
    $seconds = (int)$row2['SUMMP'] % 60;

     if ( $seconds >= 0 && $seconds < 10) {
      $secondsplayed = "0$seconds";
    } else {
        $secondsplayed = $seconds;
    }

    $SUMMP = "$minutesplayed:$secondsplayed";  
	
	if($SUMFTtotal != 0){
		$FTperc = $SUMFTscored/$SUMFTtotal;
	} else{
		$FTperc = 0;
	}

	if($SUMFGtotal != 0){
		$FGperc = $SUMFGscored/$SUMFGtotal;
	} else{
		$FGperc = 0;
	}

	if($SUMFG_outside_scored != 0){
		$FGOperc = $SUMFG_outside_scored/$SUMFG_outside_total;
	} else{
		$FGOperc = 0;
	}

	if($SUMTHPscored != 0){
		$THPperc = $SUMTHPscored/$SUMTHPtotal;
	} else{
		$THPperc = 0;
	}

	$totpointsOLD = (2*$SUMFGscored)+$SUMFTscored;

	$totpointsNEW = (2* ($SUMFGscored + $SUMFG_outside_scored))+$SUMFTscored + 3 * $SUMTHPscored;


	if ($totalgamesplayed > 0) {

			$DRBAVG = round($SUMDRB/$totalgamesplayed);
			$ORBAVG = round($SUMORB/$totalgamesplayed);
			$TOAVG = round($SUMTOV/$totalgamesplayed);
			$STAVG = round($SUMST/$totalgamesplayed);
			$ASTAVG = round($SUMAST/$totalgamesplayed);
			$BSAVG = round($SUMBS/$totalgamesplayed);
			$PFAVG = round($SUMPF/$totalgamesplayed);
			$PTSAVG = round($totpointsNEW/$totalgamesplayed);
			$S5AVG = round(($SUMS5/$totalgamesplayed)*100);
			$DNPAVG = round(($SUMDNP/$totalgamestatsavailable)*100);


			// MINUTES PLAYED CORRECT NOTATION
			$MPAVG = round((int)$row2['SUMMP']/$totalgamesplayed);
			$minutesplayed = floor((int)$MPAVG / 60);
  			$seconds = (int)$MPAVG % 60;

  			if ( $seconds >= 0 && $seconds < 10) {
		      $secondsplayed = "0$seconds";
		    } else {
		        $secondsplayed = $seconds;
		    }

  			$MPAVGprint = "$minutesplayed:$secondsplayed"; 



		} else{
				$DRBAVG = $ORBAVG = $TOAVG = $STAVG = $BSAVG = $PFAVG = $PTSAVG = $ASTAVG = $S5AVG = $DNPAVG = '0';
				$MPAVGprint = "0:00";
			}


	echo "<tr>";

	echo "	
	<td>$playernumber</td> 
	<td>$firstname</td> 
	<td>$lastname</td> 
	<td>$SUMDRB($DRBAVG)</td> 
	<td>$SUMORB($ORBAVG)</td> 
	<td>$SUMTOV($TOAVG)</td> 
	<td>$SUMST($STAVG)</td>";

	if ($setseason >= '2018/2019') { 
		echo "<td> $SUMAST($ASTAVG)</td>"; 
		echo "<td> $SUMBS($BSAVG)</td>";
	}		

	if ($setseason < '2017/2018') {
	  	echo "<td>$SUMFGscored/$SUMFGtotal(". round(($FGperc)*100) ."%)</td>";
	} else if ($setseason >= '2017/2018') {
	  	echo "<td>$SUMFGscored/$SUMFGtotal(". round(($FGperc)*100) ."%)</td>";
	  	echo "<td>$SUMFG_outside_scored/$SUMFG_outside_total(". round(($FGOperc)*100) ."%)</td>";
	  	echo "<td>$SUMTHPscored/$SUMTHPtotal(". round(($THPperc)*100) ."%)</td>";
	}

	echo "<td>$SUMFTscored/$SUMFTtotal(". round(($FTperc)*100) ."%)</td>";

	if ($setseason < '2017/2018') { 
		echo "<td>$totpointsOLD</td>";
	} else {
		echo "<td>$totpointsNEW($PTSAVG)</td>";
	}

	echo "<td>$SUMPF($PFAVG)</td>";

	if ($setseason >= '2018/2019') { 
		echo "<td> $SUMMP($MPAVGprint)</td>";
	echo "<td> $SUMS5/$totalgamesplayed($S5AVG%)</td>"; }

	echo "<td>$SUMDNP/$totalgamestatsavailable($DNPAVG%)</td>";
	echo "</tr>";

	}

	if ($setseason >= '2018/2019') { 

		$sql = "SELECT SUM(ORB) AS SUMORB, SUM(DRB) AS SUMDRB, SUM(TOV) AS SUMTOV, SUM(ST) AS SUMST, count(DISTINCT gamenumber) AS totalgamestatsavailable FROM stats WHERE season = '$setseason' AND playerID = 'team'";

		$result = $db->query($sql);
		$row = $result->fetch_assoc();

	  	$SUMORB = $row['SUMORB'];
	  	$SUMDRB = $row['SUMDRB']; 
	  	$SUMTO = $row['SUMTOV']; 
	  	$SUMST = $row['SUMST']; 

	  	$totalgamestatsavailable = $row['totalgamestatsavailable'];

	  	if ($totalgamestatsavailable > 0) { 

	  		$DRBAVG = round($SUMDRB/$totalgamestatsavailable);
			$ORBAVG = round($SUMORB/$totalgamestatsavailable);
			$TOAVG = round($SUMTO/$totalgamestatsavailable);
			$STAVG = round($SUMST/$totalgamestatsavailable);
	  	} else {
				$DRBAVG = $ORBAVG = $TOAVG = $STAVG = '0';
			}

        echo "<tr>"; 
        echo "<td></td>"; 
        echo "<td colspan='2'>Team</td>"; 
        echo "<td>$SUMDRB($DRBAVG)</td>"; 
        echo "<td>$SUMORB($ORBAVG)</td>"; 
        echo "<td>$SUMTO($TOAVG)</td>"; 
        echo "<td>$SUMST($STAVG)</td>"; 
        echo "<td></td>"; 
        echo "<td></td>"; 
        echo "<td></td>"; 
        echo "<td></td>"; 
        echo "<td></td>"; 
        echo "<td></td>"; 
        echo "<td></td>"; 
        echo "<td></td>"; 
        echo "<td></td>"; 
        echo "<td></td>"; 
        echo "<td></td>";
        echo "</tr>";  
        }

	$sql = "SELECT SUM(ORB) AS SUMORB,
		SUM(DRB) AS SUMDRB,
		SUM(TOV) AS SUMTOV,
		SUM(PF) AS SUMPF,
		SUM(ST) AS SUMST,
		SUM(AST) AS SUMAST,
		SUM(BS) AS SUMBS,
		SUM(FTscored) AS SUMFTscored,
		SUM(FTtotal) AS SUMFTtotal,
		SUM(FGscored) AS SUMFGscored,
		SUM(FGtotal) AS SUMFGtotal,
		SUM(FG_outside_scored) AS SUMFG_outside_scored,
		SUM(FG_outside_total) AS SUMFG_outside_total,
		SUM(THPscored) AS SUMTHPscored,
		SUM(THPtotal) AS SUMTHPtotal,
		count(case DNP when 'no' then 1 else null end) AS SUMDNP,
		count(DISTINCT gamenumber) AS totalgamestatsavailable 
	FROM stats 
	WHERE season = '$setseason'";

	$result=$db->query($sql);
	$row=$result->fetch_assoc();

	$SUMORB = $row['SUMORB'];
	$SUMDRB = $row['SUMDRB'];
	$SUMTOV = $row['SUMTOV'];
	$SUMST = $row['SUMST'];
	$SUMAST = $row['SUMAST'];
	$SUMBS = $row['SUMBS'];
	$SUMPF = $row['SUMPF'];
	$SUMFTtotal = $row['SUMFTtotal'];
	$SUMFTscored = $row['SUMFTscored'];

	$SUMFGscored = $row['SUMFGscored'];
	$SUMFGtotal = $row['SUMFGtotal'];

	$SUMFG_outside_scored = $row['SUMFG_outside_scored'];
	$SUMFG_outside_total = $row['SUMFG_outside_total'];

	$SUMTHPscored = $row['SUMTHPscored'];
	$SUMTHPtotal = $row['SUMTHPtotal'];

	$totalgamestatsavailable = $row['totalgamestatsavailable'];
	$SUMDNP = $totalgamestatsavailable - $row['SUMDNP'];

	$totalgamesplayed = $row2['SUMDNP'];

	$minutesplayed = floor((int)$row2['SUMMP'] / 60);
    $secondsplayed = (int)$row2['SUMMP'] % 60;
    $SUMMP = "$minutesplayed:$secondsplayed";
	
	if($SUMFTtotal != 0){
		$FTperc = $SUMFTscored/$SUMFTtotal;
	} else {
		$FTperc = 0;
	}

	if($SUMFGtotal != 0){
		$FGperc = $SUMFGscored/$SUMFGtotal;
	} else{
		$FGperc = 0;
	}

	if($SUMFG_outside_total != 0){
		$FGOperc = $SUMFG_outside_scored/$SUMFG_outside_total;
	} else{
		$FGOperc = 0;
	}

		if($SUMTHPtotal != 0){
		$THPperc = $SUMTHPscored/$SUMTHPtotal;
	} else{
		$THPperc = 0;
	}

	$totpointsOLD = (2*$SUMFGscored)+$SUMFTscored;
	$totpointsNEW = (2* ($SUMFGscored + $SUMFG_outside_scored))+$SUMFTscored + 3 * $SUMTHPscored;

	if ($totalgamestatsavailable > 0) {

			$DRBAVG = round($SUMDRB/$totalgamestatsavailable);
			$ORBAVG = round($SUMORB/$totalgamestatsavailable);
			$TOAVG = round($SUMTOV/$totalgamestatsavailable);
			$STAVG = round($SUMST/$totalgamestatsavailable);
			$ASTAVG = round($SUMAST/$totalgamestatsavailable);
			$BSAVG = round($SUMBS/$totalgamestatsavailable);
			$PFAVG = round($SUMPF/$totalgamestatsavailable);
			$PTSAVG = round($totpointsNEW/$totalgamestatsavailable);

		} else{
				$DRBAVG = $ORBAVG = $TOAVG = $STAVG = $BSAVG = $PFAVG = $PTSAVG = $ASTAVG = '0';
			}




	echo "<tr>";

	echo "
	<td></td> 
	<td></td> 
	<td><strong>TOTAL</strong></td> 
	<td><strong>$SUMDRB($DRBAVG)</strong></td> 
	<td><strong>$SUMORB($ORBAVG)</strong></td> 
	<td><strong>$SUMTOV($TOAVG)</strong></td> 
	<td><strong>$SUMST($STAVG)</strong></td>
	";

	if ($setseason >= '2018/2019') { 
		echo "<td><strong>$SUMBS($BSAVG)</strong></td>";
		echo "<td><strong>$SUMAST($ASTAVG)</strong></td>";
	}

	
	if ($setseason < '2017/2018') {
	  	echo "<td><strong>$SUMFGscored/$SUMFGtotal(". round(($FGperc)*100) ."%)</strong></td>";
	} else if ($setseason >= '2017/2018') {
	  echo "<td><strong>$SUMFGscored/$SUMFGtotal(". round(($FGperc)*100) ."%)</strong></td>";
	  echo "<td><strong>$SUMFG_outside_scored/$SUMFG_outside_total(". round(($FGOperc)*100) ."%)</strong></td>";
	  echo "<td><strong>$SUMTHPscored/$SUMTHPtotal(". round(($THPperc)*100) ."%)</strong></td>";
	  
	}

	echo "<td><strong>$SUMFTscored/$SUMFTtotal(". round(($FTperc)*100) ."%)</strong></td>";


	if ($setseason < '2017/2018') {
		echo "<td><strong>$totpointsOLD</strong></td>";
	} else if ($setseason >= '2017/2018') {
		echo "<td><strong>$totpointsNEW($PTSAVG)</strong></td>";
	}

	echo "<td><strong>$SUMPF($PFAVG)</strong></td>";
	echo "<td></td>";


	if ($setseason >= '2018/2019') {
		echo "<td></td>";
		echo "<td></td>";
	}

	echo "<td></td>";


	echo "</tr>";

}

?>

    </tbody>
  </table>

    </div>
    <div class="col-md-1"></div>
  </div>
</div>

